<?php
/*
PLUGIN NAME: Blog Summary
PLUGIN URI: http://wp-plugin-dev.googlecode.com/
DESCRIPTION: Insert a list of latest blog posts using the shortcode <code>[blog-summary count="N"]</code> (default is 5). For WordPress 2.6.x.
AUTHOR: Scott Allan Wallick
AUTHOR URI: http://scottwallick.com/
VERSION: 0.1.0 &beta;
*/

/*
BLOG SUMMARY
by SCOTT ALLAN WALLICK, http://scottwallick.com/
from PLAINTXT.ORG, http://www.plaintxt.org/

BLOG SUMMARY is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

BLOG SUMMARY is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for details.

You should have received a copy of the GNU General Public License
along with BLOG SUMMARY. If not, see www.gnu.org/licenses/.
*/

// Function for our shortcode [blog-summary count="N"], where N = count of posts.
function blog_summary_shortcode($attr) {

	// Describes what attributes to parse from shortcode; only 'count'
	extract( shortcode_atts( array(
			'count'  =>  '5', // Default value is missing 'count' attribute
		), $attr ) );

	// Queries to populate our loop based on shortcode count attribute
	$r = new WP_Query("showposts=$count&what_to_show=posts&nopaging=0&post_status=publish");

	// Only run if we have posts; can't run this through searches
	if ( $r->have_posts() && !is_search() ) :

		// Begin the output for shortcode; Open parent UL with hAtom support
		$output = '<ul class="xoxo hfeed">';

		// Begins our loop for returning posts
		while ( $r->have_posts() ) :

			// Sets which post from our loop we're at
			$r->the_post();

			// Allows the_date() with multiple posts within a single day
			unset($previousday);

			// Let's use Sandbox classes for each post LI, if available
			if ( function_exists('sandbox_post_class') ) {
				$classes = sandbox_post_class(false);
			} else { // Defaults to hentry for hAtom support
				$classes = 'hentry';
			}

			// Begin post wrapper LI
			$output .= "\n\t" . '<li id="post-' . get_the_ID() . '" class="' . $classes . '">';

			// Post title H4
			$output .= "\n\t\t" . '<h4 class="entry-title"><a href="' .  get_permalink() . '" title="' . sprintf( __( 'Permalink to %s', 'blog_summary' ), the_title_attribute('echo=0') ) . '" rel="bookmark">' . get_the_title() . '</a></h4>';

			// Post date with hAtom support
			$output .= "\n\t\t" . '<span class="entry-date"><abbr class="published" title="' . get_the_time('Y-m-d\TH:i:sO') . '">' . sprintf( __( '%s', 'blog_summary' ), the_date( '', '', '', false ) ) . '</abbr></span>';

			// Comments number
			$output .=  "\n\t\t" . '<span class="entry-comments"><a href="' . get_permalink() . '#comments" title="' . sprintf( __( 'Comments to %s', 'blog_summary' ), the_title_attribute('echo=0') ) . '">' . sprintf( __( 'Comments (%s)', 'blog_summary' ), apply_filters( 'comments_number', get_comments_number() ) ) . '</a></span>';

			// Post excerpt with hAtom support
			$output .= "\n\t\t" . '<div class="entry-summary">' . "\n" . apply_filters( 'the_excerpt', get_the_excerpt() ) . "\t\t</div>";

			// Close each post LI
			$output .= "\n\t</li>";

		endwhile; // while ( $r->have_posts() ) :

		// Close the parent UL
		$output .= "\n</ul>";

		rewind_posts(); // Rewinds loop from $r->the_post();

	endif; // if ( $r->have_posts() ) :

	wp_reset_query(); // Clears our query, $r = new WP_Query()

	return $output; // Returns $output to the shortcode
}

// Allow localization. Which I doubt will be used. (Sorry.)
load_theme_textdomain('blog_summary');

// Register the shortcode to the function blog_excerpts_shortcode()
add_shortcode( 'blog-summary', 'blog_summary_shortcode' );
?>